package com.fancy.learncenter.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.OnlyInfoBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.net.URLEncoder;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class SettingInfoActivity extends BaseActivity {

    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.tv_version)
    TextView tvVersion;
    @Bind(R.id.headerIcon)
    SimpleDraweeView headerIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_info, Status.STATUS, "设置");
        ButterKnife.bind(this);
        headerIcon.setImageURI(LoginUserSharePrefernce.getHeaderPath());
        tvVersion.setText(Utils.getVersionName());
    }

    @Override
    protected void onResume() {
        super.onResume();
        name.setText(LoginUserSharePrefernce.getName());
    }

    @OnClick({R.id.setting_header, R.id.login_out, R.id.setting_name})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.setting_header:
                intent = new Intent(this, SettingHeaderActivity.class);
                startActivity(intent);
                break;
            case R.id.setting_name:
                intent = new Intent(this, SettingNameActivity.class);
                startActivity(intent);
                break;
            case R.id.login_out:
                loginOut();
                break;
        }
    }

    private void loginOut() {
        HttpMehtod.getInstance().loginOut(new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<OnlyInfoBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), OnlyInfoBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack() {
                    @Override
                    public void success(Object dataBean) {
                        ToastUtil.show("退出成功");
                        LoginUserSharePrefernce.clearData();
                        Constant.isLogin = false;
                        LoginOutMall();

                        SPUtils.putStudentRole(false);
                        SPUtils.putTeacherRole(false);
                        SPUtils.putClassID("");
                        LoginUserSharePrefernce.putPassword("");

                        Intent intent = new Intent(SettingInfoActivity.this, MainActivity.class);
                        intent.putExtra(MainActivity.START_KEY, MainActivity.NGK_CODE);
                        startActivity(intent);


                    }

                    @Override
                    public void fail(String errorCode) {

                    }

                }).dealData();
            }
        });
    }

    //商城登录
    private void LoginOutMall() {
        WebView mWebView = new WebView(this);
        mWebView.clearHistory();
        mWebView.clearFormData();
        mWebView.clearCache(true);

        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext());
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeSessionCookie();
        cookieManager.removeAllCookie();

    }
}
